<?php

namespace app\controllers;

use wicked\core\Request;
use wicked\core\Response;

use app\models\Person;
use app\models\Event;

class PersonController extends \wicked\core\Controller
{

    public function index()
    {

    }

    public function show($id)
    {
        $person = Person::find($id);
        return $person ? compact('person') : json(false);
    }

    public function edit($id)
    {
        if(Request::hasPost())
        {
            // update person
            $data = Request::post('person');
            $person = new Person($data);
            $person->save();

            // update add event
            $events = Request::post('event');
            foreach($events as $data)
            {
                $event = new Event($data);
                $event->person = $person->id;
                $event->save();
            }

            die(1);

            Response::redirect();
        }
        else
        {
            $person = Person::findOne($id);
            return compact('person');
        }
    }

}
